package com.hw.one.collect.radar.service;

import com.alibaba.fastjson.JSONObject;
import com.hw.one.collect.mapper.RadarImageMapper;
import com.hw.one.collect.radar.config.RadarConfig;
import com.hw.one.collect.radar.po.RadarImage;
import com.hw.one.collect.radar.util.ImageUtil;
import com.hw.one.collect.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

/**
 * Created by Administrator on 2017/11/23.
 */
@Service
public class RadarPicService {


    protected Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private RadarImageMapper radarImageMapper;

    public void saveRadarPic(JSONObject jsonObject) {


        String startTime = jsonObject.getString("startTime");
        String endTime = jsonObject.getString("endTime");
        Date startDate = DateUtil.parseDateByStr(startTime, DateUtil.SDF_YMD_HMS);
        Date endDate = DateUtil.parseDateByStr(endTime, DateUtil.SDF_YMD_HMS);
        log.info("开始时间：" + startTime);
        log.info("结束时间：" + endTime);
        String datePath = startTime.substring(0, 10);
        String serverPath = RadarConfig.getInstance().get("ServerPath");
        log.info("保存路径：" + serverPath);
        String type = RadarConfig.getInstance().get("radarType");
        log.info("保存类型：" + type);

        // 图片字符串
        String base64Str = jsonObject.getString("base64");
        String base64Str1 = base64Str.split(",")[1];
        // 图片类型
        String imageType = base64Str.split(",")[0].split("/")[1].split(";")[0];
        log.info("图片类型：" + imageType);
        String uuid = jsonObject.getString("id");
        if (StringUtils.isBlank(uuid)) {
            log.info("%%%%未接收到uuid");
            uuid = "none" + UUID.randomUUID().toString();
        }
        String dataType = jsonObject.getString("dataType");
        String gridType = jsonObject.getString("type");
        String SNCode = jsonObject.getString("SNCode");
        log.info("数据类型：" + dataType);
        log.info("栅格类型：" + gridType);
        log.info("SNCode：" + SNCode);
        //  图片名称
        String imageName = File.separator + startTime.split(" ")[1].replace(":", "") + SNCode + "_" + dataType+ "_" + gridType + "." + imageType;

        // 图片路径
        String imageUrl = File.separator + type + File.separator + datePath + imageName;
        // 图片绝对路径
        String imagePath = serverPath + imageUrl;
        // 保存图片
        log.info("imagePath：" + imagePath);
        boolean flag = ImageUtil.generateImage(base64Str1, imagePath);
        RadarImage radarImage = new RadarImage();
        radarImage.setId(uuid);
        radarImage.setSitecode(jsonObject.getString("siteCode"));
        radarImage.setSncode(SNCode);
        radarImage.setDatatype(dataType);
        radarImage.setGridtype(gridType);
        if (!"0".equals(gridType)) {
            JSONObject max = jsonObject.getJSONObject("max");
            BigDecimal maxValue = max.getBigDecimal("maxValue");
            String x = max.getString("x");
            String y = max.getString("y");
            if (!StringUtils.isBlank(x) && !StringUtils.isBlank(y)) {
                radarImage.setMaxdata(maxValue);
                radarImage.setDataloc(x + "," + y);
            }
        }
        radarImage.setStartdate(startDate);
        radarImage.setEnddate(endDate);
        radarImage.setUrl(imageUrl);
        radarImage.setCreatedate(new Date());
        radarImageMapper.insertSelective(radarImage);
    }
}
